package com.ordertracker;

import com.sun.jersey.api.json.JSONConfiguration;
import com.sun.jersey.api.json.JSONJAXBContext;
import com.sun.jersey.api.json.JSONMarshaller;
import org.springframework.stereotype.Component;

import javax.ws.rs.ext.ContextResolver;
import javax.ws.rs.ext.Provider;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;

/**
 * Description of class.
 * Created 1/11/13 1:22 PM
 *
 * @author andrey.rodin@playtech.com
 */
@Component
@Provider
public class JAXBContextResolver implements ContextResolver<JAXBContext> {
    private static final String ENTITY_PACKAGE = "com.ordertracker";
    private static JSONJAXBContext JAXB_CONTEXT;

    static {
        try {
            JAXB_CONTEXT = new JSONJAXBContext(JSONConfiguration.mapped().build(), ENTITY_PACKAGE);
            JSONMarshaller jsonMarshaller = JAXB_CONTEXT.createJSONMarshaller();
            Application.getApplication().setJsonMarshaller(jsonMarshaller);
            Application.getApplication().setJsonUnmarshaller(JAXB_CONTEXT.createJSONUnmarshaller());

        } catch (JAXBException e) {
            e.printStackTrace();
        }
    }

    public JAXBContextResolver() {
    }

    @Override
    public JAXBContext getContext(Class<?> type) {
//        if (type.getPackage().getName().contains(ENTITY_PACKAGE)) {
            return JAXB_CONTEXT;
//        } else {
//            return null;
//        }
    }
}


//@Component
//@Provider
//public class JAXBContextResolver implements ContextResolver<JAXBContext> {
//    private final static String ENTITY_PACKAGE = "com.ordertracker.persistent";
//    private final static JAXBContext context;
//    static {
//        try {
//            context = new JAXBContextAdapter(new JSONJAXBContext(JSONConfiguration.mapped().rootUnwrapping(false).build(), ENTITY_PACKAGE));
//        } catch (final JAXBException ex) {
//            throw new IllegalStateException("Could not resolve JAXBContext.", ex);
//        }
//    }
//
//    public JAXBContext getContext(final Class<?> type) {
//        try {
//            if (type.getPackage().getName().contains(ENTITY_PACKAGE)) {
//                return context;
//            }
//        } catch (final Exception ex) {
//            // trap, just return null
//        }
//        return null;
//    }
//
//    public static final class JAXBContextAdapter extends JAXBContext {
//        private final JAXBContext context;
//
//        public JAXBContextAdapter(final JAXBContext context) {
//            this.context = context;
//        }
//
//        @Override
//        public Marshaller createMarshaller() {
//            Marshaller marshaller = null;
//            try {
//                marshaller = context.createMarshaller();
//                marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
//            } catch (final PropertyException pe) {
//                return marshaller;
//            } catch (final JAXBException jbe) {
//                return null;
//            }
//            return marshaller;
//        }
//
//        @Override
//        public Unmarshaller createUnmarshaller() throws JAXBException {
//            final Unmarshaller unmarshaller = context.createUnmarshaller();
//            unmarshaller.setEventHandler(new DefaultValidationEventHandler());
//            return unmarshaller;
//        }
//
//        @Override
//        public Validator createValidator() throws JAXBException {
//            return context.createValidator();
//        }
//    }
//}